package edu.lsnu.equipment_management_system.controller;

import edu.lsnu.equipment_management_system.exception.NoPowerException;
import edu.lsnu.equipment_management_system.mapper.select.paging.EquipmentInformationMapperWithPaging;
import edu.lsnu.equipment_management_system.pojo.domain.JsonData;
import edu.lsnu.equipment_management_system.pojo.domain.PageBean;
import edu.lsnu.equipment_management_system.pojo.vo.out.information_list.UseRecordVo;
import edu.lsnu.equipment_management_system.pojo.vo.out.information_list.damage.EquipmentDamageBasicVo;
import edu.lsnu.equipment_management_system.pojo.vo.out.information_list.damage.EquipmentDamageDetailVo;
import edu.lsnu.equipment_management_system.pojo.vo.out.information_list.repair.EquipmentRepairVo;
import edu.lsnu.equipment_management_system.pojo.vo.out.information_list.transfer_out.EquipmentTransferOutBasicVo;
import edu.lsnu.equipment_management_system.pojo.vo.out.information_list.transfer_out.EquipmentTransferOutDetailVo;
import edu.lsnu.equipment_management_system.pojo.vo.out.information_list.used_using.DeviceVo;
import edu.lsnu.equipment_management_system.pojo.vo.out.information_list.used_using.EquipmentVo;
import edu.lsnu.equipment_management_system.pojo.vo.out.session.UserVo;
import edu.lsnu.equipment_management_system.pojo.vo.sql_conditions.UserNoAndEquipmentNoVo;
import edu.lsnu.equipment_management_system.service.EquipmentInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @author mzy
 * @Description 设备信息的controller
 * @date 2018/11/16
 */
@RestController
public class EquipmentInformationController {
    @Autowired
    EquipmentInformationService equipmentInformationService;
    @Autowired
    EquipmentInformationMapperWithPaging equipmentInformationMapperWithPaging;

    // --------------管理员----------------
    /**
     * @Description 管理员获得所有设备的简略信息
     * @author mzy
     * @time 2018-11-24
     * @see
     * @return   java.lang.Object
     * @params  [pageNum]
     */
    @GetMapping("/api/admin/getAllEquipmentBasicInformation/{pageNum}")
    public Object getAdminAllEquipmentBasicInformationVo(@PathVariable("pageNum") Integer pageNum) {
        PageBean<DeviceVo> adminAllEquipmentBasicInformationVo =
                equipmentInformationService.getAdminAllEquipmentBasicInformationVo(pageNum);

        return JsonData.buildSuccess(0, adminAllEquipmentBasicInformationVo, "操作成功");
    }

    /**
     * @Description 管理员获得单个设备的详细信息
     * @author mzy
     * @time 2018-11-24
     * @see
     * @return   java.lang.Object
     * @params  [equipmentNo]
     */
    /*
    @GetMapping("/api/admin/getEquipmentDetailInformation/{equipmentNo}")
    public Object adminGetEquipmentDetailInformationVo(@PathVariable String equipmentNo) {
        equipmentNo = equipmentNo.trim();
        EquipmentVo equipmentVo =
                equipmentInformationService.adminGetEquipmentDetailInformationVo(equipmentNo);
        if (null == equipmentVo) {
            return JsonData.buildError("操作失败");
        } else {
            return JsonData.buildSuccess(0, equipmentVo, "操作成功");
        }
    }
    */
    // --------------普通用户--------------

    // --------------------列表和详细信息--------------------
    /**
     * @Description 得到某个设备的使用年限和最低折旧年限
     * @author mzy
     * @time 2018-11-24
     * @see 
     * @return   java.lang.Object
     * @params  [equipmentNo]
     */
    @GetMapping("/api/user/useYearAndDepreciationPeriod/{equipmentNo}")
    public Object getUseYearAndDepreciationPeriod(@PathVariable String equipmentNo, HttpSession session) {
        equipmentNo = equipmentNo.trim();
        UserVo user = (UserVo) session.getAttribute("user");
        String userNo = user.getUserNo();
        Integer equipmentUsedYear;
        Integer equipmentDepreciationPeriod;
        try {
            equipmentUsedYear = equipmentInformationService.getEquipmentUsedYear(equipmentNo, userNo);
            equipmentDepreciationPeriod = equipmentInformationService.getEquipmentDepreciationPeriod(equipmentNo, userNo);
        } catch (NoPowerException e) {
            return JsonData.buildError(e.getMessage());
        }

        Map<String, Integer>  useYearAndDepreciationPeriod = new HashMap<>();
        useYearAndDepreciationPeriod.put("usedYear", equipmentUsedYear);
        useYearAndDepreciationPeriod.put("depreciationPeriod", equipmentDepreciationPeriod);
        if (null == equipmentUsedYear ||
                null == equipmentDepreciationPeriod) {
            return JsonData.buildError("操作失败");
        } else {
            return JsonData.buildSuccess(0, useYearAndDepreciationPeriod, "操作成功");
        }

    }

    /**
     * @Description 普通用户：获取一个普通用户使用过的设备的简略信息列表
     * @author mzy
     * @time 2018-11-24
     * @see 
     * @return   java.lang.Object
     * @params  []
     */
    @GetMapping("/api/user/getUsedEquipmentBasicInformation/{pageNum}")
    public Object getUserUsedEquipmentBasicInformationVo(@PathVariable Integer pageNum, HttpSession session) {
        UserVo user = (UserVo) session.getAttribute("user");
        String userNo = user.getUserNo();
        PageBean<DeviceVo> userUsedEquipmentBasicInformationVo
                = equipmentInformationService.getUserUsedEquipmentBasicInformationVo(pageNum, userNo);

        if (null == userUsedEquipmentBasicInformationVo) {
            return JsonData.buildError("操作失败");
        } else {
            return JsonData.buildSuccess(0, userUsedEquipmentBasicInformationVo, "操作成功");
        }

    }

    /**
     * @Description 普通用户：获取一个普通用户在用的设备的简略信息列表
     * @author mzy
     * @time 2018-11-24
     * @see
     * @return   java.lang.Object
     * @params  []
     */
    @GetMapping("/api/user/getUsingEquipmentBasicInformation/{pageNum}")
    public Object getUserUsingEquipmentBasicInformationVo(@PathVariable Integer pageNum, HttpSession session) {
        UserVo user = (UserVo) session.getAttribute("user");
        String userNo = user.getUserNo();
        PageBean<DeviceVo> userUsingEquipmentBasicInformationVo =
                equipmentInformationService.getUserUsingEquipmentBasicInformationVo(pageNum, userNo);

        if (null == userUsingEquipmentBasicInformationVo) {
            return JsonData.buildError("操作失败");
        } else {
            return JsonData.buildSuccess(0, userUsingEquipmentBasicInformationVo, "操作成功");
        }
    }

    /**
     * @Description 普通用户：获取一个普通用户报损报废设备的简略信息列表
     * @author mzy
     * @time 2018-11-24
     * @see
     * @return   java.lang.Object
     * @params  []
     */
    @GetMapping("/api/user/getDamageEquipmentBasicInformation/{pageNum}")
    public Object getUserDamageEquipmentBasicInformationVo(@PathVariable Integer pageNum, HttpSession session) {
        UserVo user = (UserVo) session.getAttribute("user");
        String userNo = user.getUserNo();
        PageBean<EquipmentDamageBasicVo> userDamageEquipmentBasicInformationVo =
                equipmentInformationService.getUserDamageEquipmentBasicInformationVo(pageNum, userNo);

        if (null == userDamageEquipmentBasicInformationVo) {
            return JsonData.buildError("操作失败");
        } else {
            return JsonData.buildSuccess(0, userDamageEquipmentBasicInformationVo, "操作成功");
        }
    }

    /**
     * @Description 普通用户：获取一个普通用户调出设备的简略信息列表
     * @author mzy
     * @time 2018-11-24
     * @see
     * @return   java.lang.Object
     * @params  []
     */
    @GetMapping("/api/user/getTransferOutEquipmentBasicInformation/{pageNum}")
    public Object getUserTransferOutEquipmentBasicInformationVo(@PathVariable Integer pageNum, HttpSession session) {
        UserVo user = (UserVo) session.getAttribute("user");
        String userNo = user.getUserNo();
        PageBean<EquipmentTransferOutBasicVo> userTransferOutEquipmentBasicInformationVo =
                equipmentInformationService.getUserTransferOutEquipmentBasicInformationVo(pageNum, userNo);

        if (null == userTransferOutEquipmentBasicInformationVo) {
            return JsonData.buildError("操作失败");
        } else {
            return JsonData.buildSuccess(0, userTransferOutEquipmentBasicInformationVo, "操作成功");
        }
    }

    /**
     * @Description 普通用户：获取一个普通用户维修维护设备的简略信息列表
     * @author mzy
     * @time 2018-11-24
     * @see
     * @return   java.lang.Object
     * @params  []
     */
    @GetMapping("/api/user/getRepairEquipmentBasicInformation/{pageNum}")
    public Object getEquipmentRepairVo(@PathVariable Integer pageNum, HttpSession session) {
        UserVo user = (UserVo) session.getAttribute("user");
        String userNo = user.getUserNo();
        PageBean<EquipmentRepairVo> equipmentRepairVo =
                equipmentInformationService.getEquipmentRepairVo(pageNum, userNo);

        if (null == equipmentRepairVo) {
            return JsonData.buildError("操作失败");
        } else {
            return JsonData.buildSuccess(0, equipmentRepairVo, "操作成功");
        }
    }

    /**
     * @Description 普通用户获得单个设备的详细信息(在用、使用过)
     * @author mzy
     * @time 2018-11-24
     * @see
     * @return   java.lang.Object
     * @params  [equipmentNo]
     */
    @GetMapping({"/api/user/getEquipmentDetailInformation/{equipmentNo}",
                    "/api/admin/getEquipmentDetailInformation/{equipmentNo}"})
    public Object userGetEquipmentDetailInformationVo(@PathVariable String equipmentNo, HttpSession session) {
        equipmentNo = equipmentNo.trim();
        EquipmentVo equipmentVo = null;

        if (null != session.getAttribute("user")) {
            UserVo user = (UserVo) session.getAttribute("user");
            String userNo = user.getUserNo();

            try {
                equipmentVo = equipmentInformationService.userGetEquipmentDetailInformationVo(equipmentNo, userNo);
            } catch (NoPowerException e) {
                return JsonData.buildError(e.getMessage());
            }
        }

        if (null != session.getAttribute("admin")) {
            equipmentVo = equipmentInformationService.adminGetEquipmentDetailInformationVo(equipmentNo);
        }


        if (null == equipmentVo) {
            return JsonData.buildError("操作失败");
        } else {
            return JsonData.buildSuccess(0, equipmentVo, "操作成功");
        }
    }

    /**
     * @Description 普通用户获得单个损毁设备的详细信息（可用于生命周期的头部）
     * @author mzy
     * @time 2018-11-24
     * @see 
     * @return   java.lang.Object
     * @params  []
     */
    @GetMapping({"/api/user/getDamageEquipmentDetailInformation/{equipmentNo}",
                    "/api/admin/getDamageEquipmentDetailInformation/{equipmentNo}"})
    public Object getEquipmentDamageDetailInformationVo(@PathVariable String equipmentNo, HttpSession session) {
        equipmentNo = equipmentNo.trim();
        EquipmentDamageDetailVo equipmentDamageDetailInformationVo = null;

        if (null != session.getAttribute("user")) {
            UserVo user = (UserVo) session.getAttribute("user");
            String userNo = user.getUserNo();

            UserNoAndEquipmentNoVo userNoAndEquipmentNoVo = new UserNoAndEquipmentNoVo();
            userNoAndEquipmentNoVo.setEquipmentNo(equipmentNo);
            userNoAndEquipmentNoVo.setUserNo(userNo);

            equipmentDamageDetailInformationVo =
                    equipmentInformationMapperWithPaging.getUserEquipmentDamageDetailInformationVo(userNoAndEquipmentNoVo);
        }

        if (null != session.getAttribute("admin")) {
            equipmentInformationMapperWithPaging.getAdminEquipmentDamageDetailInformationVo(equipmentNo);
        }


        if (null == equipmentDamageDetailInformationVo) {
            return JsonData.buildError("操作失败");
        } else {
            return JsonData.buildSuccess(0, equipmentDamageDetailInformationVo, "操作成功");
        }
    }

    /**
     * @Description 普通用户获得单个调出设备的详细信息（可用于生命周期的头部）
     * @author mzy
     * @time 2018-11-25
     * @see
     * @return   java.lang.Object
     * @params  [equipmentNo, session]
     */
    @GetMapping({"/api/user/getTransferOutEquipmentDetailInformation/{equipmentNo}",
                    "/api/admin/getTransferOutEquipmentDetailInformation/{equipmentNo}"})
    public Object getEquipmentTransferOutDetailInformationVo(@PathVariable String equipmentNo, HttpSession session) {
        equipmentNo = equipmentNo.trim();
        EquipmentTransferOutDetailVo equipmentTransferOutDetailInformationVo = null;

        if (null != session.getAttribute("user")) {
            UserVo user = (UserVo) session.getAttribute("user");
            String userNo = user.getUserNo();

            UserNoAndEquipmentNoVo userNoAndEquipmentNoVo = new UserNoAndEquipmentNoVo();
            userNoAndEquipmentNoVo.setEquipmentNo(equipmentNo);
            userNoAndEquipmentNoVo.setUserNo(userNo);

            equipmentTransferOutDetailInformationVo =
                    equipmentInformationMapperWithPaging.getUserEquipmentTransferOutDetailInformationVo(userNoAndEquipmentNoVo);

        }

        if (null != session.getAttribute("admin")) {
            equipmentTransferOutDetailInformationVo =
                    equipmentInformationMapperWithPaging.getAdminEquipmentTransferOutDetailInformationVo(equipmentNo);
        }

        if (null == equipmentTransferOutDetailInformationVo) {
            return JsonData.buildError("操作失败");
        } else {
            return JsonData.buildSuccess(0, equipmentTransferOutDetailInformationVo, "操作成功");
        }
    }

    // --------------------生命周期的头部和一条条的使用记录--------------------
    /**
     * @Description 在用和使用中设备的(生命周期中的)头部信息(缩略的)
     * @author mzy
     * @time 2018-11-25
     * @see
     * @return   java.lang.Object
     * @params  []
     */
    @GetMapping("/api/user/getBasicInformationInLifeCycle/{equipmentNo}")
    public Object getBasicInformationInLifeCycleVo(@PathVariable String equipmentNo, HttpSession session) {
        equipmentNo = equipmentNo.trim();
        UserVo user = (UserVo) session.getAttribute("user");
        String userNo = user.getUserNo();
        DeviceVo basicInformationInLifeCycleVo =
                null;
        try {
            basicInformationInLifeCycleVo = equipmentInformationService.getBasicInformationInLifeCycleVo(equipmentNo, userNo);
        } catch (NoPowerException e) {
            JsonData.buildError(e.getMessage());
        }

        if (null == basicInformationInLifeCycleVo) {
            return JsonData.buildError("操作失败");
        } else {
            return JsonData.buildSuccess(0, basicInformationInLifeCycleVo, "操作成功");
        }
    }
    // 以上是在用和使用中设备的头部信息：关于报损报废和调出设备生命周期中的头部信息
    // 直接在上面的报损报废的详细信息、调出的详细信息中进行拆检。

    /**
     * @Description 不管是在用，使用过，报损报废和调出，生命周期中的使用记录都是这个！
     * @author mzy
     * @time 2018-11-25
     * @see 
     * @return   java.lang.Object
     * @params  []
     */
    @GetMapping({"/api/user/getUseRecordInLifeCycle/{equipmentNo}/{pageNum}",
                    "/api/admin/getUseRecordInLifeCycle/{equipmentNo}/{pageNum}"})
    public Object getUseRecordInLifeCycleVo(@PathVariable String equipmentNo, @PathVariable Integer pageNum, HttpSession session) {
        equipmentNo = equipmentNo.trim();

        PageBean<UseRecordVo> useRecordInLifeCycleVo = null;
        try {
            useRecordInLifeCycleVo = equipmentInformationService.getUseRecordInLifeCycleVo(pageNum, equipmentNo, session);
        } catch (NoPowerException e) {
            JsonData.buildError(e.getMessage());
        }

        if (null == useRecordInLifeCycleVo) {
            return JsonData.buildError("操作失败");
        } else {
            return JsonData.buildSuccess(0, useRecordInLifeCycleVo, "操作成功");
        }
    }

}
